<?php

if (!defined('IN_WONDERSHARE')) {
	die('Hacking attempt');
}

require('./classes/module/module_faq_new.php');

class FaqNewControl {
	
	var $faq_new_m;
	function __construct() {
	
		$this->faq_new_m = new FaqNewModule();
	}
	
	/**
	* 同步按照网站faq
	* @param   int         $new_id
	* @return  array
	*/
	function  syn_faq_new($data, $temInfo, $brand_info, $web_type) {
		
		$sql  = "DELETE FROM faq_info_new WHERE web_type = '{$web_type}' ";
		$GLOBALS['db']->query($sql);
		
		foreach ($data as $value) {
			
			if ( !empty($value->version_id) && ($value->status == 1) ) {
				
				$sql = "SELECT p.product_brand 
						FROM won_product won_p 
						LEFT JOIN product p ON won_p.won_product_id = p.product_id 
						LEFT JOIN version v ON p.product_id = v.product_id 
						WHERE FIND_IN_SET(p.product_brand,'{$brand_info}') AND 
						FIND_IN_SET(v.version_id, '{$value->version_id}') AND won_p.won_product_delete = 0 LIMIT 1 ";
				
				$nums = $GLOBALS['db']->num_rows($GLOBALS['db']->query($sql));
				
				if ($nums > 0) {
					
					  $sql = "INSERT INTO faq_info_new(id,type_id,product_id,version_id,version_name,problem_type_id, 
					  			reason_id,title,sub_title,faq_type_id,resolve_status,resolve_time,resolve_remark,status, 
					  			contents,description,created,manage_id,hits,release_ws_id,released,first_released, 
					  			keywords,ei,helpful_points,useless_points,suggestion_nums,is_top,is_delete,web_type) 
								VALUES ('".$value->id."','".$value->type_id."','".$value->product_id."',
								'".$value->version_id."' ,'".iconv('GBk', 'UTF-8', addslashes($value->version_name))."',
								'".$value->problem_type_id."','".$value->reason_id."',
								'".iconv('GBk', 'UTF-8', addslashes($value->title))."' ,'".iconv('GBk', 'UTF-8', addslashes($value->sub_title))."',
								'".$value->faq_type_id."','".$value->resolve_status."','".$value->resolve_time."', 
								'".iconv('GBk', 'UTF-8', addslashes($value->resolve_remark))."','".$value->status."',
								'".iconv('GBk', 'UTF-8', addslashes($value->contents))."','".iconv('GBk', 'UTF-8', addslashes($value->description))."' ,
								'".$value->created."','".$value->manage_id."','".$value->hits."',
								'".$value->release_ws_id."', '".$value->released."','".$value->first_released."',
								'".iconv('GBk', 'UTF-8', addslashes($value->keywords))."','".$value->ei."', '".$value->helpful_points."',
								'".$value->useless_points."','".$value->suggestion_nums."','".$value->is_top."','0',
								".$web_type." )";
					 $GLOBALS['db']->query($sql);
				}
			
			}
		}
	}
	
	/**
	* 根据id得到faq_info_new信息
	* @param   int         $new_id
	* @return  array
	*/
	function  get_new_id_info($new_id) {
		
		$p_rows = $v_rows =  array();
		$sql = "SELECT faq_type.type_name,new.id,new.title,new.resolve_status ,new.status,new.hits,new.release_ws_id,new.released , new.helpful_points,new.useless_points,new.new_sort,new.suggestion_nums,new.product_id,new.version_id , new.sub_title,new.contents,new.keywords,new.description 
				FROM faq_info_new AS new 
				LEFT JOIN faq_type ON faq_type.type_id = new.type_id 
				WHERE new.id = '{$new_id}'";
		$rows =  $this->faq_new_m->get_new_info($sql);
		
		$rows = $rows[0];
		$sql = "SELECT product.product_name,version.version_name 
				FROM version,product 
				WHERE version.product_id = product.product_id AND FIND_IN_SET(version.version_id,'{$rows['version_id']}')";
		$v_rows =  $this->faq_new_m->get_new_info($sql);
		
		foreach ($v_rows AS $v_value) {
			
			$p_rows[trim($v_value['product_name'])]['product_name'] = $v_value['product_name'];
			$version_name = empty($p_rows[trim($v_value['product_name'])]['version_name'])?$v_value['version_name']:','.$v_value['version_name'];
			$p_rows[trim($v_value['product_name'])]['version_name'][] = $version_name;
		}
		
		$rows['product_versions'] = $p_rows;
		return $rows;
	
	}

	/**
	* 得到分页相关信息
	*
	* @param   int        $page       页码
	* @param   int        $type       为1代表没有条件查询 为2代表有条件查询
	* @param   INT        $pagesize   每页显示多少条记录
	* @param   INT        $webType    网站类型
	* @param   array      $search_array   搜索数组
	* @return  array
	*/
	function get_url_info($page, $type = '', $pagesize = 10, $webType, $search_array = null ) {
		
		$url_str = $sql_faq = '';
		if ($type == 'list') { 
			
			$sql_faq = "SELECT COUNT(*) AS total FROM faq_info_new WHERE web_type = '{$webType}'";
		} else {
		
			$data = $this->get_search_condition($search_array, 'get_url', $webType);
			$url_str = $data['url_str'];
			if (!empty($search_array['p_cbsid'])) {
				
				$sql = "SELECT version_id FROM version WHERE product_cbs_id = '{$search_array['p_cbsid']}'";
				$result = $GLOBALS['db']->query($sql);
				
				while($row = $GLOBALS['db']->fetch_array($result)) {
					
					$sql = "SELECT id 
							FROM faq_info_new AS new 
							LEFT JOIN faq_type ON faq_type.type_id = new.type_id 
							WHERE FIND_IN_SET('{$row['version_id']}',version_id) AND new.web_type = '{$webType}' {$data['where_str']}";
					$sql_faq .= empty($sql_faq)?$sql:' UNION '.$sql;
				}
				$sql_faq = empty($sql_faq) ? 'SELECT COUNT( * ) AS total FROM version WHERE 1 !=1' : 'SELECT COUNT( * ) AS total FROM ( '.$sql_faq.' ) AS info ';
			
			} else  {
				$sql_faq = "SELECT COUNT(*) AS total 
							FROM faq_info_new AS new 
							LEFT JOIN faq_type ON faq_type.type_id = new.type_id 
							WHERE new.web_type = '{$webType}' {$data['where_str']} ";
			}
		}
		
		$page_info = get_page_info($sql_faq, $pagesize, $page);
		$page_url = get_page_url($page, $page_info['page_num'], $url_str);
		return  array('page_info'=>$page_info, 'page_url'=>$page_url);
	}
	
	/**
	* 得到列表faq_info_new信息
	*
	* @param   string     $type       类型（list为列表 不带搜索 而 list_s 在列表 带有搜索）
	* @param   int        $start      从那行开始
	* @param   int        $pagesize   取多少条记录
	* @param   INT        $webType    网站类型
	* @param   array      $search_array   搜索数组
	* @return  array
	*/
	function  get_new_info($type, $start = 0, $pagesize = 10, $webType, $search_array = null) {
		
		$date =  array();
		$sql_faq = '';
		if ($type == 'list') {
			
			$sql_faq = "SELECT faq_type.type_name,new.type_id,new.id,new.title,new.resolve_status ,new.status,new.hits,new.release_ws_id,new.released ,new.helpful_points,new.useless_points,new.new_sort,new.suggestion_nums , (
						SELECT is_recommend_index 
						FROM faq_operation_types 
						WHERE id = new.id AND web_type = '{$webType}') AS is_recommend_index, (
						SELECT is_important 
						FROM faq_operation_types 
						WHERE id = new.id AND web_type = '{$webType}') AS is_important, new.update_user,IF(new.update_time > 0, FROM_UNIXTIME(new.update_time, '%Y-%m-%d %H:%s'), '') update_time 
						FROM faq_info_new new 
						LEFT JOIN faq_type ON faq_type.type_id = new.type_id 
						WHERE new.web_type = '{$webType}'
						ORDER BY new.id ASC LIMIT {$start},{$pagesize} ";
									

		} else {
			$data = $this->get_search_condition($search_array, 'search_info', $webType);
			if (!empty($search_array['p_cbsid'])) {
				
				$sql = "SELECT version_id FROM version WHERE product_cbs_id = '{$search_array['p_cbsid']}'";
				$result = $GLOBALS['db']->query($sql);
				
				while ($row = $GLOBALS['db']->fetch_array($result)) {
					
					$sql = "SELECT faq_type.type_name,new.type_id,new.id,new.title,new.resolve_status ,new.status,new.hits,new.release_ws_id,new.released , new.helpful_points,new.useless_points,new.new_sort,new.suggestion_nums , (
							SELECT is_important 
							FROM faq_operation_types 
							WHERE id = new.id AND web_type = '{$webType}') AS is_important, (
							SELECT is_recommend_index 
							FROM faq_operation_types 
							WHERE id = new.id AND web_type = '{$webType}') AS is_recommend_index, new.update_user,IF(new.update_time > 0, FROM_UNIXTIME(new.update_time, '%Y-%m-%d %H:%s'), '') update_time 
							FROM faq_info_new AS new 
							LEFT JOIN faq_type ON faq_type.type_id = new.type_id 
							WHERE FIND_IN_SET('{$row['version_id']}',new.version_id) AND new.web_type = '{$webType}' {$data['where_str']} ";
					$sql_faq .= empty($sql_faq) ? $sql : ' UNION '.$sql;
				}
				if (empty($sql_faq)) {
					return array();
				}
				$sql_faq = "SELECT * FROM ( {$sql_faq} ) AS info  ORDER BY id  ASC  LIMIT {$start},{$pagesize} ";
			} else  {
				$sql_faq = "SELECT faq_type.type_name,new.type_id,new.id,new.title,new.resolve_status ,new.status,new.hits,new.release_ws_id,new.released , new.helpful_points,new.useless_points,new.new_sort,new.suggestion_nums , (
							SELECT is_important 
							FROM faq_operation_types 
							WHERE id = new.id AND web_type = '{$webType}') AS is_important, (
							SELECT is_recommend_index 
							FROM faq_operation_types 
							WHERE id = new.id AND web_type = '{$webType}') AS is_recommend_index, new.update_user,IF(new.update_time > 0, FROM_UNIXTIME(new.update_time, '%Y-%m-%d %H:%s'), '') update_time 
							FROM faq_info_new AS new 
							LEFT JOIN faq_type ON faq_type.type_id = new.type_id 
							WHERE new.web_type = '{$webType}' {$data['where_str']} 
							ORDER BY new.id ASC LIMIT {$start},{$pagesize} " ;
				
			}
		}
	
		$temArray =  $this->getRelatedTemTablte($webType);
		$result = $GLOBALS['db']->query($sql_faq);
		
		while ($row = $GLOBALS['db']->fetch_array($result)) {
			
			$sql = "SELECT (
					SELECT COUNT(*) 
					FROM faq_commet 
					WHERE new_id = '{$row['id']}' AND com_useful = 1 ) AS useful , ( 
					SELECT D_ID 
					FROM Table_{$temArray['faq_info']['table']}
					WHERE Field_{$temArray['faq_info']['temKey']} = '{$row['id']}' ) AS D_ID , ( 
					SELECT url 
					FROM Table_{$temArray['faq_info']['table']}
					WHERE Field_{$temArray['faq_info']['temKey']} = '{$row['id']}') AS url , (
					SELECT COUNT(*) 
					FROM faq_commet 
					WHERE new_id = '{$row['id']}' AND com_useful = 0 ) AS no_useful ";
			$useful_arr = $GLOBALS['db']->getAll($sql);
			
			$row['D_ID'] = empty($useful_arr[0]['D_ID']) ? 0 : $useful_arr[0]['D_ID'];
			$row['url'] = empty($useful_arr[0]['url']) ? '' : $useful_arr[0]['url'];
			$row['order_D_ID'] = 0;$row['order_url'] = '';
			
			if (empty($row['D_ID'])) {
				
				$sql = "SELECT D_ID,url 
						FROM Table_{$temArray['order_info']['table']}
						WHERE Field_{$temArray['order_info']['temKey']} = '{$row['id']}'";
				$row_order = $GLOBALS['db']->getAll($sql);
				
				$row['order_D_ID'] = $row_order[0]['D_ID'];
				$row['order_url'] = $row_order[0]['url'];
			
			}
			
			$row['helpful_points'] = $row['helpful_points'] + $useful_arr[0]['useful'];
			$row['useless_points'] = $row['useless_points'] - $useful_arr[0]['no_useful'];

			$date[] = $row;
		}
		unset($row_order, $row);
		return $date;
	}
	
	/**
	* 根据网站类型得到相关模板表信息
	* @param   INT        $webType    网站类型
	* @return  array
	*/
	function  getRelatedTemTablte($webType) {
		
		$temArray  = array();
		switch ($webType) {
			
			case 1://wondershare网站
			  $temArray['faq_info'] = array('table' => '92','temKey' => '20948','baseTitle' => '20949' ,
			  'temTitle' => '20950','temKeywords' => '20951','temDescription' => '20952');
			  
			  $temArray['order_info'] = array('table' => '79','temKey' => '20810' ,'baseTitle' => '20811' ,
			  'temTitle' => '20812','temKeywords' => '20813','temDescription' => '20814');
			  
			  $temArray['products_info'] = array('table' => '88','pId' => '20892' ,'pName' => '20893' ,
			  'pCbsId' => '21602','temTitle' => '20894','temKeywords' => '20895','temDescription' => '20896');
			  
			  $temArray['products_howtos_info'] = array('table' => '90','pId' => '20912' ,'pName' => '20913' ,
			  'pCbsId' => '21603','temTitle' => '20914','temKeywords' => '20915','temDescription' => '20916');
			  
			  $temArray['products_help_info'] = array('table' => '91','pId' => '20938' ,'pName' => '20939' ,
			  'pCbsId' => '21604','temTitle' => '20928','temKeywords' => '20929','temDescription' => '20930',
			  'temHelpId' => '20926' ,'temHelpTitle' => '20927','temHelpType' => '20935');
			  
//			  $temArray['products_video_info'] = array('table' => '93','pId' => '20961' ,'pName' => '20962' ,
//			  'pCbsId' => '','temTitle' => '20963','temKeywords' => '20964','temDescription' => '20965');
			  
			  $temArray['products_history_info'] = array('table' => '94','pId' => '20974' ,'pName' => '20975' ,
			  'pCbsId' => '21605','temTitle' => '20976','temKeywords' => '20977','temDescription' => '20978');
			  
			  $temArray['products_suggestion_info'] = array('table' => '95','pId' => '20986' ,'pName' => '20987' ,
			  'pCbsId' => '21606','temTitle' => '20990','temKeywords' => '20988','temDescription' => '20989');
			  $temArray['where_str'] = " AND product.product_brand = 'Wondershare' 
			  AND !FIND_IN_SET( product.product_id,  '87,88,89,90,91,92,99,100,101,102,103,104' ) ";
			  $temArray['product_brand'] = 'Wondershare';
			  $temArray['webDomain'] = 'support.wondershare.com';
			  
			  break;  
			  
			case 8://Iskysoft网站
		
			  
			  $temArray['order_info'] = array('table' => '151','temKey' => '21741' ,'baseTitle' => '21742' ,
			  'temTitle' => '21743','temKeywords' => '21744','temDescription' => '21745');
			  
			  $temArray['products_info'] = array('table' => '156','pId' => '21760' ,'pName' => '21762' ,
			  'pCbsId' => '21761','temTitle' => '21764','temKeywords' => '21765','temDescription' => '21766');
			  
			  $temArray['products_howtos_info'] = array('table' => '157','pId' => '21767' ,'pName' => '21769' ,
			  'pCbsId' => '21768','temTitle' => '21770','temKeywords' => '21771','temDescription' => '21772');
			  
			  $temArray['products_help_info'] = array('table' => '158','pId' => '21773' ,'pName' => '21775' ,
			  'pCbsId' => '21774','temTitle' => '21779','temKeywords' => '21780','temDescription' => '21781',
			  'temHelpId' => '21776' ,'temHelpTitle' => '21777','temHelpType' => '21778');
			  
			  $temArray['faq_info'] = array('table' => '159','temKey' => '21782','baseTitle' => '21783' ,
			  'temTitle' => '21784','temKeywords' => '21785','temDescription' => '21786');

			  $temArray['products_history_info'] = array('table' => '160','pId' => '21787' ,'pName' => '21789' ,
			  'pCbsId' => '21788','temTitle' => '21790','temKeywords' => '21791','temDescription' => '21792');
			  
			  $temArray['products_suggestion_info'] = array('table' => '163','pId' => '21799' ,'pName' => '21801' ,
			  'pCbsId' => '21800','temTitle' => '21802','temKeywords' => '21803','temDescription' => '21804');
			  
			  $temArray['where_str'] = ' AND product.product_brand = "iSkysoft" ';
			  $temArray['product_brand'] = 'iSkysoft';
			  $temArray['webDomain'] = 'support.iskysoft.com';
			  
			  break;  
			 
		
		}
		return $temArray;
	}
	
	/**
	* 得到所有FAQ分类信息
	* @return  array
	*/
	function  get_faq_category_list() {
		
		$sql =  "SELECT type_name,type_id 
				FROM faq_type 
				GROUP BY type_name 
				ORDER BY type_id ASC ";
		return $this->faq_new_m->get_new_info($sql);
	}
	
	/**
	* ajax改变值
	* @param   int      $id               FAQ文章id
	* @param   string   $change_value     值
	* @param   int      $field            要改变的数据字段
	* @param   string   $type             操作类型
	* @param   string   $webType          网站类型
	* @return  int
	*/
	function  change_info($id, $change_value, $field, $type, $webType = 1 ) {
		
		$sql = '';
		//是否推荐到首页
		if ($type == 'new_recommend' || $type == 'new_important') {
			
			$insertStr = $updateStr = ''; 
			if ($type == 'new_recommend') {
				
				$insertValue = ' 0 , 1 ';
				$updateValue = ' is_recommend_index =  '.$change_value;
				
			} else if($type == 'new_important') {
				
				$insertValue = ' 1 , 0';
				$updateValue = ' is_important =  '.$change_value;
			}
			
			$sql = "SELECT COUNT(*) AS nums 
					FROM faq_operation_types 
					WHERE id = '{$id}' AND web_type = '{$webType}' ";
			$nums =  $GLOBALS['db']->getOne($sql);
			
			if ($nums > 0) {
				$sql = "UPDATE faq_operation_types SET {$updateValue} 
						WHERE id = '{$id}' AND web_type = '{$webType}' ";
			} else {
				$sql = "INSERT INTO faq_operation_types (id,is_important,is_recommend_index,web_type) 
						VALUES ('{$id}',{$insertValue},'{$webType}')";
				
				
			}
			
		} else {
			$sql = "UPDATE faq_info_new SET {$field} = '{$change_value}' , update_user = '{$_SESSION['AName']}', update_time = '{$_SERVER['REQUEST_TIME']}' 
					WHERE id = '{$id}' ";
			
		}
	
		return $this->faq_new_m->get_new_info($sql);
	
	}
	
	/**
	* 得到在搜索的条件
	*
	* @param   array        $search_array   搜索条件
	* @return  array
	*/
	function get_search_condition($search_array, $type = '', $webType = 1) {
		
		$url_str = '&url_page=1'; $where_str = '';
		$resolve_arr  = array('0','1','2');
		if (!empty($search_array['id'])) {
			
			$where_str = ' AND  new.id = '.$search_array['id'];
			$url_str.= '&new_id='.$search_array['id'];
		} else { 
			//分类
			if (!empty($search_array['type_id'])) {
				$where_str = ' AND new.type_id = '.$search_array['type_id'];
				$url_str.= '&new_type='.$search_array['type_id'];
			}
			//问题原因
			if ($search_array['reason_id'] != 'all'  && !empty($search_array['reason_id'])) {
				$where_str .= ' AND new.reason_id = '.$search_array['reason_id'];
				$url_str.= '&new_reason_id='.$search_array['reason_id'];
			}
			//解决状态
			if ($search_array['resolve_status'] != 'all' && in_array($search_array['resolve_status'], $resolve_arr)) {
				$where_str .= ' AND  new.resolve_status = '.$search_array['resolve_status'];
				$url_str.= '&new_status='.$search_array['resolve_status'];
			}
			//是否重要
			if (in_array($search_array['is_important'], array('0', '1'))) {
				
				$in_str = $search_array['is_important'] == 1 ? ' IN ' : ' NOT IN ';
				$where_str .= ' AND  new.id '.$in_str.' (SELECT id FROM faq_operation_types WHERE  
				 web_type = '.$webType.' AND is_important = 1 ) ';
				$url_str.= '&new_important='.$search_array['is_important'];
			}
			//标题
			if (!empty($search_array['title'])) {
				$where_str .= " AND new.title LIKE '%{$search_array['title']}%'";
				$url_str.= '&new_title='.urlencode($search_array['title']);
			}
			//标题
			if (!empty($search_array['p_cbsid'])) {
				
				$url_str.= '&p_cbsid='.$search_array['p_cbsid'];
			}
		}
		
		if ($type == 'get_url') {
			return array('where_str' => $where_str, 'url_str' => $url_str);
		} else {
			return array('where_str' => $where_str);
		}

	}

}
?>